package inabit.inabitcompiler.productions.rule.expressions;

import inabit.compiler.parser.grammar.NonTerminal;
import inabit.compiler.parser.grammar.Production;
import inabit.compiler.parser.grammar.Symbol;
import inabit.compiler.parser.grammar.Terminal;
import inabit.data.expression.Expression;
import inabit.data.expression.LogicExpression;
import inabit.inabitcompiler.lexical.InabitTokenClass;
import inabit.inabitcompiler.parser.InabitSemanticAction;

/**M' -> and MM'*/
/**@author Rafael Rocha*/
public class M1_Rule extends Production{
	private NonTerminal M;
	private NonTerminal M1;
	public M1_Rule() {
		super("M'");
	}
	
	public Symbol[] production(final NonTerminal nonTerminal) {
		M = new NonTerminal("M");
		M1 = new NonTerminal("M'");
		Symbol[] symbols = {
			new Terminal(InabitTokenClass.AND),
			M,
			M1,
			new InabitSemanticAction(nonTerminal) {
				public void execute() throws Exception{
					if(M1.code.isEmpty()) {
						nonTerminal.code = M.code;
					}else {
						Expression e = new LogicExpression(
								getExpression(M.code),
								LogicExpression.AND,
								getExpression(M1.code));
						nonTerminal.code = addExpressionAndGetCode(e);
					}
				}
			}
		};
		return symbols;
	}
}
